// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.actions;

import java.util.ArrayList;
import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.designer.ui.i18n.Messages;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-26 下午11:50:30
 */
public class RunDataBaseAction extends Action implements IMenuCreator {

    class ItemSelectionListener extends SelectionAdapter {

        private final String s;

        public ItemSelectionListener(String s) {
            this.s = s;
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
        }

    }

    private Menu menu;

    private final Action startHSQLDBAction;

    public RunDataBaseAction() {
        super(Messages.getString("RunDataBaseAction.Text")); //$NON-NLS-1$
        setMenuCreator(this);
        setEnabled(true);
        setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.ACTION_STOPDB));
        startHSQLDBAction = DenguesCorePlugin.getDefault().getDesignerCoreService().createStartHsqldbServer();
    }

    public void dispose() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
     */
    public Menu getMenu(Control parent) {
        if (menu != null && !menu.isDisposed()) {
            menu.dispose();
            menu = null;
        }
        initializeMenu(parent);
        return menu;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addMenuItems".
     */
    private void initializeMenu(Control parent) {
        menu = new Menu(parent);
        List<String> dbs = new ArrayList<String>();
        for (String s : dbs) {
            MenuItem item = new MenuItem(menu, SWT.CHECK);
            item.setImage(ImageUtil.getImage(IDenguesSharedImage.VIEW_CONNECTION));
            item.setText(s);
            item.addSelectionListener(new ItemSelectionListener(s));
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
     */
    public Menu getMenu(Menu parent) {
        return null;
    }
}
